package com.lagou.cxz.controller;

import com.lagou.cxz.dao.UserDao;
import com.lagou.cxz.dao.entity.Article;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @ClassName UserController
 * @Description
 * @Author cxz
 * @Date 2020/5/5 15:47 Version V1.0
 */
@Controller
@RequestMapping("/t")
public class UserController {

    @Autowired
    UserDao userDao;

    @RequestMapping("/a")
    public String test(Model model,
            @Param("current") Integer current) {
        if (null == current || current <= 0) {
            current = 1;
        }
        Pageable of = PageRequest.of((current - 1), 2);
        int totalPage = 0;
        List<Article> articleAll = userDao.getArticleAll(of);
        long count = userDao.count();
        if (count % 2 != 0) {
            totalPage = (int) (count / 2) + 1;
        } else {
            totalPage = (current / 2);
        }
        if (totalPage == current) {
            model.addAttribute("status", 1);
        } else {
            model.addAttribute("status", 0);
        }
        model.addAttribute("articleAll", articleAll);

        model.addAttribute("page", current);

        return "client/index";
    }
}
